package com.taiyi.renaissancemall.ui.main.message;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebView;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.liaoinstan.springview.widget.SpringView;
import com.taiyi.renaissancemall.R;
import com.taiyi.renaissancemall.adapter.AdapterManager;
import com.taiyi.renaissancemall.adapter.baseadapter.CommonAdapter;
import com.taiyi.renaissancemall.base.BaseActivity;
import com.taiyi.renaissancemall.bean.AgreementsBean;
import com.taiyi.renaissancemall.bean.MessageBean;
import com.taiyi.renaissancemall.bean.PersonInfoBean;
import com.taiyi.renaissancemall.bean.VerifiedBean;
import com.taiyi.renaissancemall.ui.main.personshare.PersonShareActivity;
import com.taiyi.renaissancemall.ui.mine.order.MyOrderActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MessageActivity extends BaseActivity<MessageView, MessagePresent> implements MessageView, View.OnClickListener {

    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.messageCenterTv)
    TextView messageCenterTv;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv)
    TextView tv;
    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.registerIntegralTv)
    TextView registerIntegralTv;
    @BindView(R.id.v1)
    View v1;
    @BindView(R.id.tv2)
    TextView tv2;
    @BindView(R.id.signInIntegralTv)
    TextView signInIntegralTv;
    @BindView(R.id.v2)
    View v2;
    @BindView(R.id.tv3)
    TextView tv3;
    @BindView(R.id.inviteIntegralTv)
    TextView inviteIntegralTv;
    @BindView(R.id.v3)
    View v3;
    @BindView(R.id.tv4)
    TextView tv4;
    @BindView(R.id.shoppingIntegralTv)
    TextView shoppingIntegralTv;
    @BindView(R.id.v4)
    View v4;
    @BindView(R.id.tv5)
    TextView tv5;
    @BindView(R.id.reviewsIntegralTv)
    TextView reviewsIntegralTv;
    @BindView(R.id.v5)
    View v5;
    @BindView(R.id.tv6)
    TextView tv6;
    @BindView(R.id.shareRecommendIntegralTv)
    TextView shareRecommendIntegralTv;
    @BindView(R.id.v6)
    View v6;
    @BindView(R.id.tv7)
    TextView tv7;
    @BindView(R.id.endorsementGoodsIntegralTv)
    TextView endorsementGoodsIntegralTv;
    @BindView(R.id.v7)
    View v7;
    @BindView(R.id.tv8)
    TextView tv8;
    @BindView(R.id.signStateTv)
    TextView signStateTv;
    @BindView(R.id.watchVideoIntegralTv)
    TextView watchVideoIntegralTv;
    @BindView(R.id.pointsRule)
    TextView pointsRule;
    @BindView(R.id.pointsRuleTv)
    WebView pointsRuleTv;
    @BindView(R.id.taskTv)
    TextView taskTv;
    @BindView(R.id.shareNumTv)
    TextView shareNumTv;
    @BindView(R.id.scrollView)
    ScrollView scrollView;
    @BindView(R.id.springView)
    SpringView springView;
    @BindView(R.id.top)
    ConstraintLayout top;

    private Dialog shareDialog;
    private String mySignType = "";

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(top).init();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_message;
    }

    @Override
    public MessagePresent initPresenter() {
        return new MessagePresent();
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        titleTv.setText("消息");
        messageCenterTv.setSelected(true);
        shareDialog = new Dialog(this, R.style.MyDialog);
        View view = LayoutInflater.from(this).inflate(R.layout.good_share_dialog_layout, null);
        shareDialog.setContentView(view);
        TextView sharePosterTv = view.findViewById(R.id.sharePosterTv);
        TextView shareLinkTv = view.findViewById(R.id.shareLinkTv);
        sharePosterTv.setText("分享商品");
        shareLinkTv.setText("个人分享");
        sharePosterTv.setOnClickListener(this);
        shareLinkTv.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        showProgress();
        presenter.getMessageList();
        presenter.getTaskInfo();
        presenter.getAgreement();
    }

    @Override
    public void initEvent() {

    }


    @OnClick({R.id.backIv, R.id.messageCenterTv, R.id.taskTv, R.id.signStateTv, R.id.shareNumTv, R.id.toPromoteTv, R.id.goShoppingTv, R.id.toCommentTv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.goShoppingTv:
            case R.id.backIv:
                finish();
                break;
            case R.id.messageCenterTv:
                recyclerView.setVisibility(View.VISIBLE);
                scrollView.setVisibility(View.GONE);
                messageCenterTv.setSelected(true);
                taskTv.setSelected(false);
                break;
            case R.id.taskTv:
                scrollView.setVisibility(View.VISIBLE);
                recyclerView.setVisibility(View.GONE);
                messageCenterTv.setSelected(false);
                taskTv.setSelected(true);
                break;
            case R.id.toPromoteTv:
                startActivity(new Intent(this, PersonShareActivity.class));
                break;
            case R.id.toCommentTv:
                Intent intent = new Intent(this, MyOrderActivity.class);
                intent.putExtra("type", 3);
                startActivity(intent);
                break;
            case R.id.signStateTv:
                showProgress();
                presenter.toSign();
                break;
            case R.id.shareNumTv:
                shareDialog.show();
                break;

        }
    }

    @Override
    public String getPage() {
        return "1";
    }

    @Override
    public void getMessageListSuccess(MessageBean messageBean) {
        hideProgress();
        List<MessageBean.MsglistBean> msglist = messageBean.getMsglist();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        CommonAdapter messageListAdapter = AdapterManager.getMessageListAdapter(this, msglist);
        recyclerView.setAdapter(messageListAdapter);
    }

    @Override
    public void getMessageListFailure(String msg) {
        hideProgress();
        toast(msg);
    }

    @Override
    public void getTaskSuccess(PersonInfoBean dayTaskBean) {
        signInIntegralTv.setText("连续签到" + dayTaskBean.getContinuousDay() + "天");
        if (dayTaskBean.getIfbuqian().equals("1")) {
            signStateTv.setText("补签");
            mySignType = "1";
        } else {
            String ifdrqian = dayTaskBean.getIfdrqian();
            if (ifdrqian.equals("2")) {//1已签2没签
                signStateTv.setText("签到");
                mySignType = "2";
            } else {
                signStateTv.setText("已签到");
                signStateTv.setTextColor(getResources().getColor(R.color.color_76665F));
                signStateTv.setBackground(getResources().getDrawable(R.drawable.f5f4f9_background_radius_5_drawable));
                signStateTv.setEnabled(false);
            }
        }

        if ("10".equals(dayTaskBean.getSharenum())) {
            shareNumTv.setBackground(getResources().getDrawable(R.drawable.f5f4f9_background_radius_5_drawable));
            shareNumTv.setEnabled(false);
            shareNumTv.setText("已完成");
        } else {
            shareNumTv.setText("分享(" + dayTaskBean.getSharenum() + "/10)");
        }

    }

    @Override
    public void getTaskFailure(String msg) {

    }

    @Override
    public void toSignSuccess(VerifiedBean verifiedBean) {
        hideProgress();
        presenter.getTaskInfo();
    }

    @Override
    public void toSignFailure(String msg) {
        hideProgress();
        toast(msg);
    }

    @Override
    public String getSignType() {
        return mySignType;
    }

    @Override
    public void getAgreementSuccess(AgreementsBean agreementsBean) {
        if (agreementsBean.jinfenrule != null) {
            pointsRuleTv.loadDataWithBaseURL(null, agreementsBean.jinfenrule, "text/html", "utf-8", null);
        }
    }

    @Override
    public void onFailure(String msg) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sharePosterTv://分享商品
                shareDialog.dismiss();
                finish();
                break;
            case R.id.shareLinkTv://个人分享
                shareDialog.dismiss();
                startActivity(new Intent(this, PersonShareActivity.class));
                break;
        }
    }
}
